<?php
/**
 * Created by Dumitru Russu
 * User: developer
 * Date: 11/2/12
 * Time: 9:21 AM
 * To change this template use File | Settings | File Templates.
 */
class Page_Be_Locale_DeliveryMethods extends Page {
	/**
	 * @var Db_Locale_DeliveryMethods
	 */
	protected $tableObject;

	function __construct() {
		$this->idTableObject = get('id', 'int');
		$this->classTableObject = 'Db_Locale_DeliveryMethods';
		$this->actionsTableObject = array(
			'addEditAction',
			'saveAction'
		);
		parent::__construct();
	}

	public function defaultAction(){
		$this->addBreadcrumb(t(T_FORM_BUTTON, 'add'), '?action=addEdit');

		$deliveryMethods = Db_Locale_DeliveryMethods::findAll();
		self::$TPL->assign('deliveryMethods', $deliveryMethods);

		self::$TPL->setBodyTemplate('list_delivery_methods');
		return array(
			'type' => 'display'
		);
	}

	public function addEditAction() {
		perm('DELIVERY_METHODS_EDIT', self::$PAGE['p_url']);

		$deliveryTypes = Db_Locale_DeliveryMethods::getEnumValues('dm_type');
		self::$TPL->assign('deliveryTypes', $deliveryTypes);

		if(!empty($this->tableObject->objectArray->id)) {
			$this->addBreadcrumb(t(T_FORM_BUTTON, 'edit') . ' "'. ct(T_DELIVERY_METHODS_TITLE, $this->tableObject->objectArray->dm_name) . '"', '?action=addEdit&id=' . $this->tableObject->objectArray->id, true);
			self::$TPL->assign('deliveryMethod', $this->tableObject->objectArray);
		} else {
			$this->addBreadcrumb(t(T_FORM_BUTTON, 'add'), '?action=addEdit', true);
		}
		self::$TPL->setBodyTemplate('add_edit_delivery_method');
		return array(
			'type' => 'display'
		);
	}

	public function saveAction() {
		perm('DELIVERY_METHODS_EDIT', self::$PAGE['p_url']);
		$this->message = t(T_FORM_MSG, 'success');
		$this->tableObject->dm_name = strtolower(post('dm_name', 'str'));
		$this->tableObject->dm_type = post('dm_type', 'str');

		if(!empty($this->tableObject->dm_name) && !empty($this->tableObject->dm_type)) {
			if(!$this->tableObject->save(true)) {
				$this->error = true;
				$this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
			}
		}

		return array(
			'type' => 'redirect',
			'query' => false
		);
	}

	public function deleteAction() {
		perm('DELIVERY_METHODS_EDIT', self::$PAGE['p_url']);
		$this->message = t(T_FORM_MSG, 'success');

		if(!Db_Locale_DeliveryMethods::abstractDelete($this->idTableObject)) {
			$this->error = true;
			$this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
		}

		return array(
			'type' => 'redirect',
			'query' => false
		);
	}

}
